package com.pengshun.modules.api.mapper;

import com.pengshun.modules.api.transport.dto.*;
import com.pengshun.modules.domain.FreightSettlement;
import com.pengshun.modules.domain.TransportAssign;
import com.pengshun.modules.domain.TransportTakeCarriage;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public interface TransportMapper {

    /**
     * 查询货源订单列表
     * @param page
     * @param size
     * @param status
     * @param userId
     * @return
     */
    List<TransportOrderDto> getOrderListByStatus(@Param("page") Integer page, @Param("size") Integer size , @Param("status") Integer status, @Param("userId") Long userId);

    /**
     * 货运订单详情
     * @param orderAllotTransportId
     * @return
     */
    TransportOrderDto getOrderDetil(@Param("orderAllotTransportId")Long orderAllotTransportId);

    /**
     * 货运指定运输公司表添加数据
     * @param transportAssigns
     * @return
     */
    int insertAssignsList(List<TransportAssign> transportAssigns);

    /**
     * 删除指定运输公司数据
     * @param transportId
     * @return
     */
    @Delete("delete from ps_transport_assign where transport_id=#{transportId}")
    int delAssignsList(@Param("transportId") Long transportId);

    /**
     * 查询运单详情
     * @param transportId
     * @return
     */
    TransportDto getTransportDetilById(@Param("transportId") Long transportId);

    /**
     * 查询货运顶部信息
     * @return
     */
    TransportDto selectTopInfo(@Param("nowTime") Date nowTime,@Param("page") int page,@Param("size") Integer size);

    /**
     * 条件查询货运信息列表
     * @param page
     * @param size
     * @param sort
     * @param infoType
     * @param freightTypeCode
     * @param freightTransportCode
     * @param vehicleTypeId
     * @param vehicleLengthId
     * @return
     */
    List<TransportDto> selectUserListBy(@Param("page") int page, @Param("size") Integer size, @Param("sort") Integer sort, @Param("infoType") Integer infoType,
                                        @Param("freightTypeCode")String freightTypeCode, @Param("freightTransportCode") String freightTransportCode,
                                        @Param("vehicleTypeId") Long vehicleTypeId, @Param("vehicleLengthId")Long vehicleLengthId, @Param("longitude")Double longitude,
                                        @Param("latitude")Double latitude, @Param("shipProvinceAdcode")String shipProvinceAdcode, @Param("shipCityAdcode")String shipCityAdcode,
                                        @Param("shipDistrictAdcode")String shipDistrictAdcode, @Param("receiveProvinceAdcode")String receiveProvinceAdcode,
                                        @Param("receiveCityAdcode")String receiveCityAdcode, @Param("receiveDistrictAdcode")String receiveDistrictAdcode
                                        , @Param("merchantId")Long merchantId, @Param("showPosition") Integer showPosition,@Param("name") String name);

    /**
     * 状态查询商家发布列表
     * @param page
     * @param size
     * @param status
     * @param userId
     * @return
     */
    List<TransportDto> getTransportListByStatus(@Param("page") Integer page, @Param("size") Integer size , @Param("status") Integer status, @Param("userId") Long userId);

    /**
     * 车主接单列表
     * @param page
     * @param size
     * @param status
     * @param userId
     * @return
     */
    List<TransportOrderDto> getTakeListByStatus(@Param("page") Integer page, @Param("size") Integer size , @Param("status") Integer status, @Param("userId") Long userId);

    /**
     * 司机接单列表
     * @param page
     * @param size
     * @param status
     * @param userId
     * @param transportTakeOrderId
     * @return
     */
    List<TransportOrderDto> getDriverTakeList(@Param("page") Integer page, @Param("size") Integer size , @Param("status") Integer status,
                                                      @Param("userId") Long userId, @Param("transportTakeOrderId")Long transportTakeOrderId);

    /**
     * 车主分配的司机接单列表
     * @param page
     * @param size
     * @param status
     * @param userId
     * @param transportTakeOrderId
     * @return
     */
    List<TransportOrderDto> getCarBossAllortDriverTakeList(@Param("page") Integer page, @Param("size") Integer size , @Param("status") Integer status,
                                                      @Param("userId") Long userId, @Param("transportTakeOrderId")Long transportTakeOrderId);

    /**
     * 车主 接单详情
     * @param transportTakeOrderId
     * @return
     */
    TransportOrderDetil selectTakeOrderDetil(@Param("transportTakeOrderId")Long transportTakeOrderId);

    /**
     * 司机接单详情
     * @param transportTakeCarriageId
     * @return
     */
    TransportOrderDetil selectDriverTakeOrderDetil(@Param("transportTakeCarriageId") Long transportTakeCarriageId);

    /**
     * 车辆接单详情
     * @param TransportTakeCarriageId
     * @return
     */
    TransportOrderDetil selectOwnerDetil(@Param("TransportTakeCarriageId")Long TransportTakeCarriageId);

    /**
     * 货运重量减库存
     * @param id
     * @param nowCoalNumber
     * @param oldCoalNumber
     * @return
     */
    @Update("update ps_transport set total_number=#{nowCoalNumber} where transport_id=#{id} and total_number=#{oldCoalNumber} ")
    int updateTransportWeight(@Param("id")Long id, @Param("nowCoalNumber")BigDecimal nowCoalNumber, @Param("oldCoalNumber")BigDecimal oldCoalNumber);

    /**
     * 查询未分配列表
     * @param id
     * @param transportTakeOrderId
     * @return
     */
    List<CarNumberDto> findAllotList(@Param("id") Long id, @Param("transportTakeOrderId") Long transportTakeOrderId);

    /**
     *已分配列表
     * @param transportTakeOrderId
     * @return
     */
    List<CarNumberDto> findAlreadyAllotList(@Param("transportTakeOrderId") Long transportTakeOrderId);



    TransportDto selectDetilBYTransportTakeOrderId(@Param("transportTakeOrderId") Long transportTakeOrderId);


    List<TakeCarriageDto> selectTakeList(@Param("page") Integer page, @Param("size") Integer size, @Param("id") Long id, @Param("status") Integer status);


    /**
     * 货运接单表减库存
     * @param oldNum
     * @param newNum
     * @param transportTakeOrderId
     * @return
     */
    @Update("update ps_transport_take_order set number=#{newNum} where number=#{oldNum} and transport_take_order_id=#{transportTakeOrderId}")
    int updateTransportTakeOrderNumber(@Param("oldNum") BigDecimal oldNum, @Param("newNum") BigDecimal newNum, @Param("transportTakeOrderId") Long transportTakeOrderId);

    List<TransportOrderDto> getTakeListByStatusAndCar(@Param("page") Integer page, @Param("size") Integer size , @Param("status") Integer status, @Param("userId") Long userId);

    /**
     * 司机结算列表
     * @param page
     * @param size
     * @param status
     * @param userId
     * @return
     */
    List<SettlementDto> driverFreightSettlementList(@Param("page") Integer page, @Param("size") Integer size, @Param("status") Integer status, @Param("userId") Long userId);

    /**
     * 运输公司结算列表
     * @param page
     * @param size
     * @param status
     * @param userId
     * @return
     */
    List<SettlementDto> transporFreightSettlementList(@Param("page") Integer page, @Param("size") Integer size, @Param("status") Integer status, @Param("userId") Long userId);


    /**
     * 根据货运id查询 是否有未结算人员
     * @param transportId
     * @return
     */
    @Select("SELECT COUNT(1) " +
            "FROM  `ps_transport_take_order` tto " +
            "LEFT JOIN `ps_transport_take_carriage` tc ON tto.`transport_take_order_id`=tc.`transport_take_order_id` " +
            "WHERE tto.`transport_id`=#{transportId} AND tto.`type`=1  AND tc.`is_apply`=0")
    Integer findIsNotSettlementUserById(@Param("transportId") Long transportId);

    /**
     * 查询未结算费用的司机
     * @param transportId
     * @return
     */
    @Select("SELECT COUNT(1) " +
            "FROM  `ps_transport_take_order` tto " +
            "LEFT JOIN `ps_transport_take_carriage` tc ON tto.`transport_take_order_id`=tc.`transport_take_order_id` " +
            "WHERE tto.`transport_id`=#{transportId} AND tto.`type`=1  AND tc.`clean_status`=1")
    Integer findIsNotSettlementUserFeeById(@Param("transportId") Long transportId);

    /**
     * 查询结算列表
     * @param transportId
     * @return
     */
    List<FreightSettlementDto> findSettlementById(@Param("transportId") Long transportId);

    /**
     * 查询运输公司申请结算列表
     * @param userId
     * @param status
     * @param page
     * @param size
     * @return
     */
    List<TransportSettlementDto> findTransportSettlementList(@Param("userId") Long userId, @Param("status") Integer status, @Param("page") Integer page, @Param("size") Integer size);

    /**
     * 查询经销商结算列表
     * @param userId
     * @param status
     * @param page
     * @param size
     * @return
     */
    List<TransportSettlementDto> findMerchantSettlementList(@Param("userId") Long userId, @Param("status") Integer status, @Param("page") Integer page, @Param("size") Integer size);

    /**
     * 根据用户类型查询是否可以接单
     * @param userType
     * @return
     */
    @Select("SELECT  take FROM  ps_user_take WHERE user_type=#{userType}")
    Integer findUserTake(@Param("userType") Integer userType);

    /**
     * 查询当前车辆可以接取的最大数量
     * @param carOwnerId
     * @return
     */
    @Select("SELECT  IFNULL(SUM(pc.`car_load`),0) FROM  ps_car_owner   pc " +
            "WHERE  pc.`car_owner_id`=#{carOwnerId}  AND pc.`status`=2 AND pc.`car_status`=1")
    BigDecimal canTakeNum(@Param("carOwnerId") Long carOwnerId);

    /**
     * 查询运输中的货运数量
     * @param carOwnerNum
     * @return
     */
    @Select("SELECT  COUNT(1) FROM  ps_transport_take_carriage  WHERE car_owner_num=#{carOwnerNum} AND `status` BETWEEN 1 AND 2")
    Integer findInTransportByCarnum(@Param("carOwnerNum") String carOwnerNum);

    /**
     * 查询用户有无运输中的货运
     * @param userId
     * @return
     */
    @Select("SELECT  COUNT(1) FROM  ps_transport_take_carriage  WHERE user_id=#{userId} AND `status` BETWEEN 1 AND 2")
    Integer findInTransportByUserId(@Param("userId") Long userId);

    /**
     * 司机异常运单
     * @param page
     * @param size
     * @param status
     * @param userId
     * @return
     */
    List<SettlementDto> driverAbnormalList(@Param("page") Integer page, @Param("size") Integer size, @Param("status") Integer status, @Param("userId") Long userId);

    /**
     * 运输公司异常运单
     * @param page
     * @param size
     * @param status
     * @param userId
     * @return
     */
    List<SettlementDto> transportAbnormalList(@Param("page") Integer page, @Param("size") Integer size, @Param("status") Integer status, @Param("userId") Long userId);

    List<TransportSettDto> settList(@Param("page") Integer page, @Param("size") Integer size, @Param("id") Long id, @Param("status") Integer status);

    @Delete("DELETE FROM `ps_freight_settlement_price`  WHERE `freight_settlement_id`=#{freightSettlementId}")
    int deletePrice(@Param("freightSettlementId") Long freightSettlementId);
}
